package com.probox.common.entity.service.customer.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;
import java.util.List;

import com.probox.common.core.enums.DataStatus;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.utils.StringUtils;
import com.probox.common.entity.domain.customer.RechargeInfo;
import com.probox.common.entity.domain.customer.RechargeInfoRights;
import com.probox.common.entity.mapper.customer.RechargeInfoRightsMapper;
import com.probox.common.entity.service.customer.IRechargeInfoRightsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 充值对应的权益Service业务层处理
 *
 * @author usoft
 * @date 2022-09-14
 */
@Service
@Transactional
public class RechargeInfoRightsServiceImpl extends ServiceImpl<RechargeInfoRightsMapper, RechargeInfoRights> implements IRechargeInfoRightsService {
    @Autowired
    private RechargeInfoRightsMapper rechargeInfoRightsMapper;


    /**
     * 查询充值对应的权益
     *
     * @param rightId 充值对应的权益ID
     * @return 充值对应的权益
     */
    @Override
    public RechargeInfoRights selectRechargeInfoRightsById(Long rightId) {
        return this.getById(rightId);
    }

    /**
     * 查询充值对应的权益列表
     *
     * @param rechargeInfoRights 充值对应的权益
     * @return 充值对应的权益
     */
    @Override
    public List<RechargeInfoRights> queryList(RechargeInfoRights rechargeInfoRights) {
        QueryWrapper<RechargeInfoRights> queryWrapper = Wrappers.query();
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfoRights.getRightId())) && rechargeInfoRights.getRightId() != null) {
            queryWrapper.eq("right_id", rechargeInfoRights.getRightId());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfoRights.getRechargeId())) && rechargeInfoRights.getRechargeId() != null) {
            queryWrapper.eq("recharge_id", rechargeInfoRights.getRechargeId());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfoRights.getRightType())) && rechargeInfoRights.getRightType() != null) {
            queryWrapper.eq("right_type", rechargeInfoRights.getRightType());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfoRights.getRightObjectId())) && rechargeInfoRights.getRightObjectId() != null) {
            queryWrapper.eq("right_object_id", rechargeInfoRights.getRightObjectId());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfoRights.getRightSort())) && rechargeInfoRights.getRightSort() != null) {
            queryWrapper.eq("right_sort", rechargeInfoRights.getRightSort());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfoRights.getRightAmount())) && rechargeInfoRights.getRightAmount() != null) {
            queryWrapper.eq("right_amount", rechargeInfoRights.getRightAmount());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfoRights.getDataStatus())) && rechargeInfoRights.getDataStatus() != null) {
            queryWrapper.eq("data_status", rechargeInfoRights.getDataStatus());
        }
        queryWrapper.ne("data_status", DataStatus.delete.getKey());
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfoRights.getCreateUser())) && rechargeInfoRights.getCreateUser() != null) {
            queryWrapper.eq("create_user", rechargeInfoRights.getCreateUser());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfoRights.getCreateName())) && rechargeInfoRights.getCreateName() != null) {
            queryWrapper.eq("create_name", rechargeInfoRights.getCreateName());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfoRights.getUpdateUser())) && rechargeInfoRights.getUpdateUser() != null) {
            queryWrapper.eq("update_user", rechargeInfoRights.getUpdateUser());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfoRights.getUpdateName())) && rechargeInfoRights.getUpdateName() != null) {
            queryWrapper.eq("update_name", rechargeInfoRights.getUpdateName());
        }
        return rechargeInfoRightsMapper.selectList(queryWrapper);
    }

    @Override
    public Integer queryCount(RechargeInfoRights rechargeInfoRights) {
        QueryWrapper<RechargeInfoRights> queryWrapper = Wrappers.query();
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfoRights.getRightId())) && rechargeInfoRights.getRightId() != null) {
            queryWrapper.eq("right_id", rechargeInfoRights.getRightId());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfoRights.getRechargeId())) && rechargeInfoRights.getRechargeId() != null) {
            queryWrapper.eq("recharge_id", rechargeInfoRights.getRechargeId());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfoRights.getRightType())) && rechargeInfoRights.getRightType() != null) {
            queryWrapper.eq("right_type", rechargeInfoRights.getRightType());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfoRights.getRightObjectId())) && rechargeInfoRights.getRightObjectId() != null) {
            queryWrapper.eq("right_object_id", rechargeInfoRights.getRightObjectId());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfoRights.getRightSort())) && rechargeInfoRights.getRightSort() != null) {
            queryWrapper.eq("right_sort", rechargeInfoRights.getRightSort());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfoRights.getRightAmount())) && rechargeInfoRights.getRightAmount() != null) {
            queryWrapper.eq("right_amount", rechargeInfoRights.getRightAmount());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfoRights.getDataStatus())) && rechargeInfoRights.getDataStatus() != null) {
            queryWrapper.ne("data_status", rechargeInfoRights.getDataStatus());
        }
        queryWrapper.ne("data_status", DataStatus.delete.getKey());
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfoRights.getCreateUser())) && rechargeInfoRights.getCreateUser() != null) {
            queryWrapper.eq("create_user", rechargeInfoRights.getCreateUser());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfoRights.getCreateName())) && rechargeInfoRights.getCreateName() != null) {
            queryWrapper.eq("create_name", rechargeInfoRights.getCreateName());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfoRights.getUpdateUser())) && rechargeInfoRights.getUpdateUser() != null) {
            queryWrapper.eq("update_user", rechargeInfoRights.getUpdateUser());
        }
        if (StringUtils.isNotEmpty(String.valueOf(rechargeInfoRights.getUpdateName())) && rechargeInfoRights.getUpdateName() != null) {
            queryWrapper.eq("update_name", rechargeInfoRights.getUpdateName());
        }
        return rechargeInfoRightsMapper.selectCount(queryWrapper);
    }

    /**
     * 新增充值对应的权益
     *
     * @param rechargeInfoRights 充值对应的权益
     * @return 结果
     */
    @Override
    public boolean insertRechargeInfoRights(RechargeInfoRights rechargeInfoRights) {
        UpdateWrapper<RechargeInfoRights> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("recharge_Id", rechargeInfoRights.getRechargeId());
        updateWrapper.set("DATA_STATUS", DataStatus.delete.getKey());
        updateWrapper.set("UPDATE_User", SecurityUtils.getUserId());
        updateWrapper.set("UPDATE_NAME", SecurityUtils.getUsername());
        updateWrapper.set("UPDATE_TIME", DateUtil.date());
        update(updateWrapper);
        rechargeInfoRights.setDataStatus(DataStatus.valid.getKey());
        rechargeInfoRights.setCreateName(SecurityUtils.getUsername());
        rechargeInfoRights.setCreateUser(SecurityUtils.getUserId());
        rechargeInfoRights.setCreateTime(DateUtil.date());
        return this.save(rechargeInfoRights);

    }


    /**
     *
     * @param rechargeInfo
     */
    @Override
    public void addList(RechargeInfo rechargeInfo){
        List<RechargeInfoRights> rightObjectIdList = rechargeInfo.getRightObjectIdList();
        UpdateWrapper<RechargeInfoRights> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("recharge_Id", rechargeInfo.getRechargeId());
        updateWrapper.set("DATA_STATUS", DataStatus.delete.getKey());
        updateWrapper.set("UPDATE_User", SecurityUtils.getUserId());
        updateWrapper.set("UPDATE_NAME", SecurityUtils.getUsername());
        updateWrapper.set("UPDATE_TIME", DateUtil.date());
        update(updateWrapper);
        if(CollectionUtil.isNotEmpty(rightObjectIdList)){
            rightObjectIdList.forEach(rech->{
                rech.setRechargeId(rechargeInfo.getRechargeId());
                rech.setDataStatus(DataStatus.valid.getKey());
                rech.setCreateName(SecurityUtils.getUsername());
                rech.setCreateUser(SecurityUtils.getUserId());
                rech.setCreateTime(DateUtil.date());
                this.save(rech);
            });
        }
    }

    /**
     * 修改充值对应的权益
     *
     * @param rechargeInfoRights 充值对应的权益
     * @return 结果
     */
    @Override
    public boolean updateRechargeInfoRights(RechargeInfoRights rechargeInfoRights) {

        return this.updateById(rechargeInfoRights);
    }

    /**
     * 批量删除充值对应的权益
     *
     * @param rightIds 需要删除的充值对应的权益ID
     * @return 结果
     */
    @Override
    public boolean deleteRechargeInfoRightsByIds(Long[] rightIds) {
        UpdateWrapper<RechargeInfoRights> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("right_id", Arrays.asList(rightIds));
        updateWrapper.set("DATA_STATUS", DataStatus.delete.getKey());
        updateWrapper.set("UPDATE_User", SecurityUtils.getUserId());
        updateWrapper.set("UPDATE_NAME", SecurityUtils.getUsername());
        updateWrapper.set("UPDATE_TIME", DateUtil.date());
        return this.update(updateWrapper);
    }


}
